#include "shell.h"

#include "FreeRTOS.h"
#include "task.h"
#include "hal_motor.h"
#include <string.h>

//extern TaskHandle_t handles_array[2];

void show_task_stack() {

//    SHELL_TypeDef *shell = shellGetCurrent();
//    if(!shell)
//        return ;
//    shellPrint(shell,"tick : %ld\r\n", xTaskGetTickCount());
//    for(int i = 0; i < 3;i++) {
//        shellPrint(shell,"[%s] :",(char *)pcTaskGetName(handles_array[i]));
//        shellPrint(shell,"free : %ld \r\n",(long)uxTaskGetStackHighWaterMark(handles_array[i]));
//    }
}
SHELL_EXPORT_CMD(stack,show_task_stack,Display Task Stack);

void motor_test_func(int argc,char *argv[]) {

    SHELL_TypeDef *shell = shellGetCurrent();
    if(!shell)
        return ;
    
    if(argc < 2) {
        goto motor_func_error;
    }

    if(strcmp(argv[1],"status") == 0) {
        uint8_t work_mode,work_status;
        g_motor_.get_status(&work_mode,&work_status);

        if(work_mode)
            shellPrint(shell,"Motor Running -> ");
        else 
            shellPrint(shell,"Motor Stoping -> ");

        if(work_status) {
            shellPrint(shell,"[UP] ");
        } else {
            shellPrint(shell,"[DOWN] ");
        }

        shellPrint(shell,"Time : %ld\r\n",g_motor_.run_time());
        return ;
    }

    if(strcmp(argv[1],"up") == 0) {
        g_motor_.ctrl_up();
        g_motor_.ctrl_run(1);
        return ;
    }

    if(strcmp(argv[1],"down") == 0) {
        g_motor_.ctrl_down();
        g_motor_.ctrl_run(1);
        return ;
    }

    if(strcmp(argv[1],"stop") == 0) {
        g_motor_.ctrl_run(0);
        return ;
    }

motor_func_error:
    shellPrint(shell,"usage motor <status|up|down|stop>\r\n");
}
SHELL_EXPORT_CMD(motor,motor_test_func,Motor Function Test);
